
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import chi2_contingency

# Load data
df = pd.read_csv("pspb_behavioral_data.csv")

# Descriptive stats
print("Condition Counts:")
print(df.groupby(['political_identity', 'moral_frame']).size())

print("\nChoice Distribution:")
print(df['choice'].value_counts().sort_index())

# Crosstab for chi-square
contingency = pd.crosstab([df['political_identity'], df['moral_frame']], df['choice'])
print("\nContingency Table:")
print(contingency)

# Chi-square test
chi2, p, dof, expected = chi2_contingency(contingency)
print(f"\nChi-square test result: chi2 = {chi2:.2f}, p = {p:.4f}, dof = {dof}")
print("Expected frequencies:")
print(expected)

# Plot
sns.countplot(data=df, x='choice', hue='political_identity')
plt.title('Behavioral Choice by Political Identity')
plt.xlabel('Choice (1=Report, 2=Share, 3=Donate)')
plt.ylabel('Count')
plt.legend(title='Political Identity')
plt.tight_layout()
plt.savefig("choice_by_identity.png")
plt.show()
